package com.pti.usermanager.web.controller;

import com.github.pagehelper.PageInfo;
import com.pti.usermanager.dao.po.RolePo;
import com.pti.usermanager.enums.ConstantCode;
import com.pti.usermanager.service.api.RoleService;
import com.pti.usermanager.web.dto.request.*;
import com.pti.usermanager.web.dto.response.ResultResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.nio.file.Path;

/**
 * @ClassName: RoleController
 * @Author: mahuilin
 * @Description: 角色控制层
 * @Date: 2019/1/17
 */
@RestController
@Api(value = "角色管理接口",tags="提供角色的管理接口")
public class RoleController {

    private static final Logger logger = LogManager.getLogger(RoleController.class);


    @Autowired
    private RoleService roleService;

    @GetMapping("/role/getRoleList")
    @ApiOperation(value = "获取角色列表" , notes = "获取角色列表",produces = "application/json")
    public ResultResponse getRoleAll(@RequestParam(value = "page",defaultValue = "0",required = false) Integer page,
                                   @RequestParam(value = "pageSize",defaultValue = "10",required = false) Integer pageSize,
                                   @RequestParam(value = "roleNm",required = false) String roleNm){

        logger.debug("page:{} pageSize:{} roleNm:{}",page,pageSize,roleNm);

        PageInfo<RolePo> all = roleService.findAll(roleNm, page, pageSize);
        ResultResponse resultResponse = new ResultResponse();
        resultResponse.setTotal((int)all.getTotal());
        resultResponse.setData(all.getList());
        return resultResponse;
    }

    @GetMapping("/role/getRoleDetail/{roleId}")
    @ApiOperation(value = "获取角色对象" , notes = "获取角色对象",produces = "application/json")
    public ResultResponse getRoleDetail(@PathVariable("roleId") Long roleId){
        logger.debug("roleId:{}",roleId);
        return roleService.findAllByRoleId(roleId);
    }

    @PostMapping("/role/saveRole")
    @ApiOperation(value = "添加角色列表" , notes = "添加角色列表",produces = "application/json")
    public ResultResponse saveRole(@RequestBody SaveRoleRequest saveRoleRequest){
        return roleService.saveRole(saveRoleRequest);
    }

    @PutMapping("/role/updateRole")
    @ApiOperation(value = "修改角色列表" , notes = "修改角色列表",produces = "application/json")
    public ResultResponse updateRole(@RequestBody UpdateRoleRequest updateRoleRequest){
        return roleService.updateRole(updateRoleRequest);
    }

    @DeleteMapping("/role/delRole")
    @ApiOperation(value = "删除角色" , notes = "删除角色",produces = "application/json")
    public ResultResponse delRole(@RequestBody DeleteRoleRequest deleteRoleRequest){
        return roleService.delRole(deleteRoleRequest);
    }

    @PostMapping("/role/assignUser")
    @ApiOperation(value = "分配用户",notes = "分配用户",produces = "application/json")
    public ResultResponse assignUser(@RequestBody SaveUserByRidAndUid saveUserByRidAndUid){
        return roleService.saveUserByRidAndUid(saveUserByRidAndUid);
    }

    @PostMapping("/role/assignMenu")
    @ApiOperation(value = "分配菜单",notes = "分配菜单",produces = "application/json")
    public ResultResponse assignMenu(@RequestBody SaveMenuByRidAndUid saveMenuByRidAndUid){
        return roleService.saveMenuByRidAndUid(saveMenuByRidAndUid);
    }

    @GetMapping("/role/aggignUserDetail/{roleId}")
    @ApiOperation(value = "分配用户详情",notes = "分配用户详情",produces = "application/json")
    public ResultResponse aggignUserDetail(@PathVariable("roleId") Long roleId){
        return null;
    }

    @GetMapping("/role/delAggignUserDetail/{roleId}")
    @ApiOperation(value = "删除分配用户详情",notes = "删除分配用户详情",produces = "application/json")
    public ResultResponse delAggignUserDetail(@RequestBody DeleteUserRequest deleteUserRequest){
        return null;
    }
}
